package com.example.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bookstore.common.Result;
import com.example.bookstore.dto.BookCategoryTreeDto;
import com.example.bookstore.entity.Cart;
import com.example.bookstore.entity.Category;
import com.example.bookstore.exception.CustomException;
import com.example.bookstore.mapper.CartMapper;
import com.example.bookstore.mapper.CategoryMapper;
import com.example.bookstore.service.CartService;
import com.example.bookstore.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public void addBigCategory() {
        Category category = new Category();
        category.setName("大分类");
        category.setParentId(1);
        category.setSort(0);
        categoryMapper.insert(category);
    }

    @Override
    public void addSmallCategory(Integer parentId) {
        if (parentId==null){
            throw new CustomException("id为空");
        }
        Category category=new Category();
        category.setName("小分类");
        category.setParentId(parentId);
        category.setSort(0);
        categoryMapper.insert(category);
    }

    @Override
    public List<BookCategoryTreeDto> selectAllTreeNodes() {
        List<BookCategoryTreeDto> categoryParent = categoryMapper.selectTreeNodeParent();//获取1级分类
        categoryParent.stream().forEach(item->{
            Integer id = item.getId();//获取1级分类的id
            List<BookCategoryTreeDto> categoryChild = categoryMapper.selectTreeNodeChild(id);//查询该id下的子分类
            item.setChildrenTreeNodes(categoryChild);
        });
        return categoryParent;
    }

    @Override
    public List<BookCategoryTreeDto> selectCategoryChild(int id) {
        List<BookCategoryTreeDto> bookCategoryTreeDtos = categoryMapper.selectTreeNodeChild(id);
//        //将list转map,以备使用,排除根节点
//        Map<Integer, BookCategoryTreeDto> mapTemp = bookCategoryTreeDtos.stream().filter(item-> id !=(item.getId())).collect(Collectors.toMap(key -> key.getId(), value -> value, (key1, key2) -> key2));
//        //最终返回的list
//        List<BookCategoryTreeDto> categoryTreeDtos = new ArrayList<>();
//        //依次遍历每个元素,排除根节点
//        bookCategoryTreeDtos.stream().filter(item->id!=(item.getId())).forEach(item->{
//            if(item.getParentId().equals(id)){
//                categoryTreeDtos.add(item);
//            }
//            //找到当前节点的父节点
//            BookCategoryTreeDto bookCategoryTreeDto = mapTemp.get(item.getParentId());
//            if(bookCategoryTreeDto!=null){
//                if(bookCategoryTreeDto.getChildrenTreeNodes() ==null){
//                    bookCategoryTreeDto.setChildrenTreeNodes(new ArrayList<BookCategoryTreeDto>());
//                }
//                //下边开始往ChildrenTreeNodes属性中放子节点
//                bookCategoryTreeDto.getChildrenTreeNodes().add(item);
//            }
//        });
        return bookCategoryTreeDtos;

    }

    @Override
    public List<Category> selectCategoryByParentId(Integer parentId) {
        LambdaUpdateWrapper<Category> lqw=new LambdaUpdateWrapper<>();
        lqw.eq(Category::getParentId, parentId);
        List<Category> categories = categoryMapper.selectList(lqw);
        return categories;
    }

    @Override
    public String findNameByMtAndSt(Integer mt, Integer st) {
        LambdaUpdateWrapper<Category> lqw=new LambdaUpdateWrapper<>();
        lqw.eq(Category::getId, mt);
        Category mtCategory = categoryMapper.selectOne(lqw);
        String mtName = mtCategory.getName();
        LambdaUpdateWrapper<Category> lqw1=new LambdaUpdateWrapper<>();
        lqw1.eq(Category::getId, st);
        Category stCategory = categoryMapper.selectOne(lqw1);
        String stName = stCategory.getName();
        return mtName+"/"+stName;
    }

//    @Override
//    public List<C> selectCategoryParent() {
//        List<BookCategoryTreeDto> bookCategoryTreeDtos = categoryMapper.selectTreeNodeParent();
////        List<Map<Integer, String>> mapList = new ArrayList<>();
////        bookCategoryTreeDtos.stream().forEach(item->{
////            Map<Integer, String> map=new HashMap<>();
////            map.put(item.getId(),item.getName());
////            mapList.add(map);
////        });
//        return bookCategoryTreeDtos;
//    }

    @Override
    public Result<String> updateCategory(Category category) {
        int i = categoryMapper.updateById(category);
        if (i>0){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @Override
    public Result<String> deleteCategory(Integer id) {
        LambdaUpdateWrapper<Category> lqw=new LambdaUpdateWrapper<>();
        lqw.eq(Category::getId,id);
        int delete = categoryMapper.delete(lqw);
        if (delete>0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
